
--物品名称
local map = require "maps.map"
local mt = ac.skill['CoinMonster']

--图标
mt.art = [[ReplaceableTextures\CommandButtons\BTNChestOfGold.blp]]

--说明
mt.tip = [[
    召唤小金币怪物, 击杀获得%gold%info
]]

--物品类型
mt.item_type = '道具'

--物品等级
mt.level = 1

--附魔价格
mt.gold = 0

--物品唯一
mt.unique = true

mt.trg = nil

local _item2MonsterName = {
    ['小金币怪'] = '少女B',
    ['大金币怪'] = '少女A',
    ['小木头怪'] = '萌物A',
    ['大木头怪'] = '萌物B',
    ['小修为怪'] = '大火元素',
    ['大修为怪'] = '小地穴蜘蛛',
}

local _summonMons = {}
local rectIdx = -1
local hero = nil
local itemName = ""

--属性
function mt:on_add()
end

function mt:on_remove()
	self.trg:remove()
end

function mt:on_click(player, name)
	if not player then
		return
	end
	hero = player.hero
	itemName = name
	self:refresh(player:get())
end

function mt:refresh(idx)
    local monster_name = _item2MonsterName[itemName]
    if not monster_name then
        print('未注册练功房怪物 道具名称: ', itemName)
		return
    end
	self:clear()
	for i = 1, 20 do
		local u = g_playerMgr.com[2]:create_unit(_item2MonsterName[itemName], map.rects['练功区域'][idx])
		table.insert(_summonMons, u)
	end
	rectIdx = idx
end

function mt:clear()
	for _, v in ipairs(_summonMons) do
		v:remove()
	end
	_summonMons = {}
end

function mt:HasAlive()
	local ret = false
	if rectIdx and hero then
		local rect = map.rects['练功区域'][rectIdx]
		local units = ac.selector():in_rect(rect):is_enemy(hero):get()
		ret = #units > 0
	end
	return ret
end

ac.loop(1000, function()
	if rectIdx < 0 then return end

	if mt:HasAlive() then
		return
	end

	mt:refresh(rectIdx)
end)
